#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
  int maximumUnits(vector<vector<int>> &boxTypes, int truckSize) {
    if (boxTypes.empty() || truckSize == 0) {
      return 0;
    }
    long long int res = 0;
    auto cmp = [](const vector<int> &a, const vector<int> &b) -> bool {
      if (a[1] < b[1])
        return false;
      return true;
    };
    sort(boxTypes.begin(), boxTypes.end(), cmp);
    
    for (auto i = boxTypes.begin(); i != boxTypes.end(); ++i) {
      int num = min(truckSize, (*i)[0]);
      res += num * (*i)[1];
      truckSize -= num;
      if (truckSize <= 0) {
        break;
      }
    }
    return res;
  }
};

int main(int argc, char *argv[]) {
  Solution s;
  vector<vector<int>> a = {{1, 3}, {2, 2}, {3, 1}};
  cout << s.maximumUnits(a, 4) << endl;
  return 0;
}
